using System;
using System.IO;
using System.Text;
using System.Windows;
using Qplus.beans;
using System.Collections.Generic;
using Qplus.Infrastructure;
using System.Diagnostics;

namespace Qplus.Commands
{

    /**
     * 
     *
     */
    public class GetUserListCommand : AbsHeaderHttpCommand , HttpInputStreamProcessor{

	    const String COMMAND = "GetUserList";
	
	    int type;

        public event EventHandler<MessageReceivedFromServer> GetUserList_0_CommandReceivedFromHttpServer;
        public event EventHandler<MessageReceivedFromServer> GetUserList_1_CommandReceivedFromHttpServer;
        public event EventHandler<MessageReceivedFromServer> GetUserList_2_CommandReceivedFromHttpServer;

	    /**
	     * @param command
	     * @param uid
	     * @param UKEY
	     */
	    public GetUserListCommand(String uid, String UKEY)
            : base(COMMAND, uid, UKEY)
        {
		    
	    }
	
	    /**
	     * @param command
	     * @param uid
	     * @param UKEY
	     */
	    public GetUserListCommand(long uid, byte[] UKEY)
            : base(COMMAND, Convert.ToString(uid), Encoding.UTF8.GetString(UKEY, 0, UKEY.Length))
        {
		    
	    }
	
	    /**
	     * 获取类型(1 推荐用户 2 在线用户)
	     * @param type
	     */
	    public void set(int type){
		    setType(type);
		    this.type = type;
	    }
	
	    private void setType(int type){
		    addParams("Type", type);
	    }

	    
	    public override Boolean processInputStream(Stream responseStream) {
		    //TODO
            return true;
	    }

	    
	    public override void onSuccess(int code) {
	    }


        public override void onFailed(int code)
        {
	    }
	
	    /**
	     * 
	     * @param type (1 推荐用户 2 在线用户)
	     * @param list
	     */
	    public void onGetUserList(int type, List<UserInfo> list){
	    }
	
	    public HttpTask execute(String url) {
            base.MessageReceivedFromHttpServer += new EventHandler<MessageReceivedFromServer>(GetUserListCommand_MessageReceivedFromHttpServer);
		    return base.execute(url, this);
	    }

        void GetUserListCommand_MessageReceivedFromHttpServer(object sender, MessageReceivedFromServer e)
        {
            //throw new NotImplementedException();
            string data = System.Text.UTF8Encoding.UTF8.GetString(e.Message, 0, e.Message.Length);
            Debug.WriteLine(data);
            if (0 == type)
                ProcessIncomingMessage_0(data);
            else if (1 == type)
                ProcessIncomingMessage_1(data);
            else if (2 == type)
                ProcessIncomingMessage_2(data);
        }
        private void ProcessIncomingMessage_0(string ReceivedMessage)
        {
            // Future hook for handling the message in somewhere else.
            // It's most probably wise to put the parsing logic in some other class.
            if (GetUserList_0_CommandReceivedFromHttpServer != null)
                GetUserList_0_CommandReceivedFromHttpServer(this, new MessageReceivedFromServer(ReceivedMessage));
        }
        private void ProcessIncomingMessage_1(string ReceivedMessage)
        {
            // Future hook for handling the message in somewhere else.
            // It's most probably wise to put the parsing logic in some other class.
            if (GetUserList_1_CommandReceivedFromHttpServer != null)
                GetUserList_1_CommandReceivedFromHttpServer(this, new MessageReceivedFromServer(ReceivedMessage));
        }
        private void ProcessIncomingMessage_2(string ReceivedMessage)
        {
            // Future hook for handling the message in somewhere else.
            // It's most probably wise to put the parsing logic in some other class.
            if (GetUserList_2_CommandReceivedFromHttpServer != null)
                GetUserList_2_CommandReceivedFromHttpServer(this, new MessageReceivedFromServer(ReceivedMessage));
        }
    }
}
